#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "material_type.h"
#include <iostream>
#include <QFile>
#include <QTextStream>
#include <QSettings>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    readSettings();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readSettings()
{
    QSettings setting("nc","settings");
    setting.beginGroup("Dialog");
    QString x=setting.value("x").toString();
    QString y=setting.value("y").toString();
    QString z=setting.value("z").toString();
    int index = setting.value("comboBox").toInt();
    ui->comboBox->setCurrentIndex(index);
    ui->lineEdit_x->setText(x);
    ui->lineEdit_y->setText(y);
    ui->lineEdit_z->setText(z);

    setting.endGroup();
}

void MainWindow::writeSettings()
{
    //location ~/.config/
    QSettings setting("nc","settings");
    setting.beginGroup("Dialog");
    setting.setValue("comboBox",ui->comboBox->currentIndex());
    setting.setValue("x",ui->lineEdit_x->text());
    setting.setValue("y",ui->lineEdit_y->text());
    setting.setValue("z",ui->lineEdit_z->text());

    setting.endGroup();
}

void MainWindow::generateGcode()
{

    material_type* mytype= new material_type();
    mytype->x=ui->lineEdit_x->text();
    mytype->y=ui->lineEdit_y->text();
    mytype->z=ui->lineEdit_z->text();

    //save
    QFile f("gcode.txt");
    if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        std::cout << "Open failed." << std::endl;
        return ;
    }

    QTextStream out(&f);

    out << "G00 "<<"X"<<mytype->x <<" 0.0"<< endl;
    out << "G01 "<<"Y"<<mytype->y << endl;
    out << "G03 "<<"Z"<<mytype->z << endl;

    f.flush();

    f.close();
}

void MainWindow::on_buttonGenerateGcode_clicked()
{
    ui->label_4->setText("gcode.txt saved!");
    this->generateGcode();
}

void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
    if(arg1 ==(ui->comboBox->itemText(1))) //type1
    {
        ui->label_4->setText("type 1 selected!");
        material_type* type1= new material_type("1","2","3");
        ui->lineEdit_x->setText(type1->x);
        ui->lineEdit_y->setText(type1->y);
        ui->lineEdit_z->setText(type1->z);


    }
    if(arg1 ==(ui->comboBox->itemText(2))) //type2
    {
        ui->label_4->setText("type 2 selected!");
        material_type* type2= new material_type("4","5","6");
        ui->lineEdit_x->setText(type2->x);
        ui->lineEdit_y->setText(type2->y);
        ui->lineEdit_z->setText(type2->z);
    }
    if(arg1 ==(ui->comboBox->itemText(3))) //type3
    {
        ui->label_4->setText("type 3 selected!");
        material_type* type3= new material_type("7","8","9");
        ui->lineEdit_x->setText(type3->x);
        ui->lineEdit_y->setText(type3->y);
        ui->lineEdit_z->setText(type3->z);
    }
    if(arg1 ==(ui->comboBox->itemText(0))) //none
    {
        ui->label_4->setText("none selected!");
        ui->lineEdit_x->setText("");
        ui->lineEdit_y->setText("");
        ui->lineEdit_z->setText("");
    }

}

void MainWindow::closeEvent(QCloseEvent *)
{
    writeSettings();
}



